 
#ifndef __COM_ADS1110_H__
#define __COM_ADS1110_H__
 
#ifdef __cplusplus
extern "C" {
#endif
 
#include <stdint.h>
#include <stddef.h>

#define ADS_DEBUG 1 //1:打开调试 0:关闭调试

#if ADS_DEBUG
    #define ADS1110_PRINTF Serial.printf  //改
    #define ADS1110_MESSAGE(fmt,args...) do{ADS1110_PRINTF(fmt"\n",##args);}while(0)
    #define ADS1110_ERR_CHECK(_err_code) do{\
        int e = (_err_code);\
             if (e == COM_ERROR_NULL)        ADS1110_MESSAGE("NULL ERROR #file %s #line %d ,at\""#_err_code"\"",__FILE__,__LINE__);\
        else if (e == COM_ERROR_INVALID_DATA)ADS1110_MESSAGE("INVALID DATA ERROR #file %s #line%d ,at\""#_err_code"\"",__FILE__,__LINE__);\
        else if (e == COM_ERROR_SEND_FAILED) ADS1110_MESSAGE("SEND FAILED ERROR #file %s #line%d ,at\""#_err_code"\"",__FILE__,__LINE__);\
        else if (e == COM_ERROR_RECV_FAILED) ADS1110_MESSAGE("RECV FAILED ERROR #file %s #line%d ,at\""#_err_code"\"",__FILE__,__LINE__);\
    }while(0)

#else
    #define ADS1110_MESSAGE(fmt,args...) do{}while(0)
    #define ADS1110_ERR_CHECK(_err_code) do{_err_code;}while(0)
#endif

typedef enum
{
    COM_SUCCESS             = 0,
    COM_ERROR_NULL          = 1,
    COM_ERROR_INVALID_DATA  = 2,
    COM_ERROR_SEND_FAILED   = 3,
    COM_ERROR_RECV_FAILED   = 4
}COM_DEV_RES;
 
/** @brief ADS1110Ax device address */
typedef enum
{
    COM_DEV_ADS1110_ADDR_A0 = 0x48,    ///< ADS1110A0 device address, 8bit
    COM_DEV_ADS1110_ADDR_A1 = 0x92,    ///< ADS1110A1 device address, 8bit
    COM_DEV_ADS1110_ADDR_A2 = 0x94,    ///< ADS1110A2 device address, 8bit
    COM_DEV_ADS1110_ADDR_A3 = 0x96,    ///< ADS1110A3 device address, 8bit
    COM_DEV_ADS1110_ADDR_A4 = 0x98,    ///< ADS1110A4 device address, 8bit
    COM_DEV_ADS1110_ADDR_A5 = 0x9A,    ///< ADS1110A5 device address, 8bit
    COM_DEV_ADS1110_ADDR_A6 = 0x9C,    ///< ADS1110A6 device address, 8bit
    COM_DEV_ADS1110_ADDR_A7 = 0x9D,    ///< ADS1110A7 device address, 8bit
}COM_DEV_ADS1110_ADDRESS_T;
 
/** @brief ADS1110Ax RATE*/
typedef enum
{    
    COM_DEV_ADS1110_RATE_240SPS = 0,   ///< 0
    COM_DEV_ADS1110_RATE_60SPS  = 1,   ///< 1
    COM_DEV_ADS1110_RATE_30SPS  = 2,   ///< 2
    COM_DEV_ADS1110_RATE_15SPS  = 3,   ///< 3
 
}COM_DEV_ADS1110_RATE_T;
 
/** @brief ADS1110Ax GAIN*/
typedef enum
{    
    COM_DEV_ADS1110_GAIN_1  = 0,       ///< 0
    COM_DEV_ADS1110_GAIN_2  = 1,       ///< 1
    COM_DEV_ADS1110_GAIN_3  = 2,       ///< 2
    COM_DEV_ADS1110_GAIN_4  = 3,       ///< 3
}COM_DEV_ADS1110_GAIN_T;
 
/** @brief ADS1110Ax MODE*/
typedef enum
{    
    COM_DEV_ADS1110_MODE_CONTINU  = 0, ///< 0  continuous conversion  0Bxxx0,xxxx
    COM_DEV_ADS1110_MODE_SINGLE   = 1, ///< 1  single conversion      0Bxxx1,xxxx
}COM_DEV_ADS1110_MODE_T;
 
/** @brief ADS1110Ax xxx CONFIG*/
typedef struct
{
    COM_DEV_ADS1110_RATE_T rate_i;   ///< ADS1110A0 rate
    COM_DEV_ADS1110_GAIN_T gain_i;   ///< ADS1110A0 gain
    COM_DEV_ADS1110_MODE_T mode_i;   ///< ADS1110A0 mode
}COM_DEV_ADS1110_CONFIG_T;
/**
* @brief IIC write callback function
* @param dev_addr: device address
* @param pdata: write data pointer
* @param size: write size
* @retval error code
*/
typedef int (*com_ads1110_write_callback_t)(uint8_t dev_addr, uint8_t *pdata, uint8_t size);
 
/**
* @brief IIC read callback function
* @param dev_addr: device address
* @param pdata: write data pointer
* @param size: write size
* @retval error code
*/
typedef int (*com_ads1110_read_callback_t)(uint8_t dev_addr, uint8_t *pdata, uint8_t size);
 
/** @brief ADS1110Ax device data struct. */
typedef struct{
    uint8_t dev_addr;                             ///< device address
    uint8_t tm_reg[3];                            ///< data of Output Register (Read Only), 
    uint8_t cfg_reg[1];                           ///< data of configuration Register to be written
    uint8_t cfg_reg_t[1];                         ///< data of configuration Register
    float   res_val[1];                           ///< resistance_value
    COM_DEV_ADS1110_CONFIG_T config[1]; 
    com_ads1110_write_callback_t w_cb;            ///< pointer of write callback
    com_ads1110_read_callback_t r_cb;             ///< pointer of read callback
}com_ads1110_dev_t;
 
/**
* @brief ads1110 init;  
* @param dev: pointer to ads1110 device
* @param dev_addr: 
* @param w_cb: write callback function
* @param r_cb: read callback function
* @retval error code
*/
int com_ads1110_init(com_ads1110_dev_t *dev, uint8_t dev_addr, com_ads1110_write_callback_t w_cb, com_ads1110_read_callback_t r_cb);
 
/**    
* @brief ads1110 write cfg register buffer data to device
* @param dev: pointer to ads1110 device
* @retval error code
*/
int com_ads1110_write_cfg_reg(com_ads1110_dev_t *dev);
 
/**
* @brief ads1110 read device data to cfg register buffer
* @param dev: pointer to ads1110 device
* @retval error code
*/
int com_ads1110_read_cfg_reg(com_ads1110_dev_t *dev);
 
/**
* @brief ads1110 read voltage
* @param dev: pointer to ads1110 device
* @param mv: output mV    
* @retval error code
*/
int com_ads1110_read_voltage(com_ads1110_dev_t *dev, float *mv);
 
/**
* @brief ads1110 read current
* @param dev: pointer to ads1110 device
* @param ma: output mA   
* @retval error code
*/
int com_ads1110_read_current(com_ads1110_dev_t *dev, float *ma);
 
/**
* @brief ads1110 set rate
* @param dev: pointer to ads1110 device
* @param rate: rate config   
* @retval error code
*/
 
/**
* @brief ADS1110 set rate mode,
* @param dev: pointer to ADS1110 device
* @param rate: input fault rate mode 
* @retval error code
*/
int com_ads1110_set_rate(com_ads1110_dev_t *dev,COM_DEV_ADS1110_RATE_T  rate);
 
/**
* @brief ADS1110 get rate mode,
* @param dev: pointer to ADS110 device
* @param rate: output rate mode 
* @retval error code
*/
int com_ads1110_get_rate(com_ads1110_dev_t *dev,COM_DEV_ADS1110_RATE_T *rate);
 
/**
* @brief ADS1110 set gain mode,
* @param dev: pointer to ADS1110 device
* @param gain: input fault gain mode 
* @retval error code
*/
int com_ads1110_set_gain(com_ads1110_dev_t *dev,COM_DEV_ADS1110_GAIN_T  gain);
 
/**
* @brief ADS1110 get gain mode,
* @param dev: pointer to ADS110 device
* @param gain: output gain mode 
* @retval error code
*/
int com_ads1110_get_gain(com_ads1110_dev_t *dev,COM_DEV_ADS1110_GAIN_T *gain);
 
/**
* @brief ADS1110 set work mode,
* @param dev: pointer to ADS1110 device
* @param gain: input fault work mode 
* @retval error code
*/
int com_ads1110_set_mode(com_ads1110_dev_t *dev,COM_DEV_ADS1110_MODE_T  mode);
 
/**
* @brief ADS1110 get work mode,
* @param dev: pointer to ADS110 device
* @param mode: output work mode 
* @retval error code
*/
int com_ads1110_get_mode(com_ads1110_dev_t *dev,COM_DEV_ADS1110_MODE_T *mode);
 
/**
* @brief ADS1110 set resistance_value,
* @param dev: pointer to ADS1110 device
* @param v: input fault resistance_value 
* @retval error code
*/
int com_ads1110_set_res_val(com_ads1110_dev_t *dev,float v);
 
/**
* @brief ADS1110 get resistance_value,
* @param dev: pointer to ADS110 device
* @param v: output resistance_value
* @retval error code
*/
int com_ads1110_get_res_val(com_ads1110_dev_t *dev,float *v);

 
#ifdef __cplusplus
}
#endif
 
#endif //__COM_ADS1110_H__
 
